package com.example.service.impl;

import com.example.mapper.InventoryMapper;
import com.example.model.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.service.InventoryService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private InventoryMapper inventoryMapper;

    @Transactional(rollbackFor = Exception.class)
    public Boolean deduct(String commodityCode, Integer count) {

        if (commodityCode.equals("product-2")) {
            throw new RuntimeException("异常:模拟业务异常:stock branch exception");
        }

        Stock stock = inventoryMapper.selectStockByCode(commodityCode);
        if(stock == null){
            throw new RuntimeException("异常:模拟业务异常1:stock branch exception");
        }
        stock.setCount(stock.getCount()-count);
        int rows = inventoryMapper.updateStockById(stock);
        return rows > 0 ? true:false;
    }
}
